package android.medicalcloud.com.yiapp.yilian.ui.view.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.medicalcloud.com.yiapp.R;
import android.medicalcloud.com.yiapp.base.utils.ListUtil;
import android.medicalcloud.com.yiapp.base.utils.Logger;
import android.medicalcloud.com.yiapp.yilian.ui.fragment.DateFragment;
import android.medicalcloud.com.yiapp.yilian.utils.android.IntentTool;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.roomorama.caldroid.CaldroidGridAdapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import hirondelle.date4j.DateTime;

/**
 * Created by linxi on 16-12-6.
 */
public class CalderAdapter extends CaldroidGridAdapter {

    public CalderAdapter(Context context, int month, int year,
                         Map<String, Object> caldroidData,
                         Map<String, Object> extraData) {
        super(context, month, year, caldroidData, extraData);

        selectDay = getToday().getYear()+"-"+getToday().getMonth()+"-"+getToday().getDay();
    }





    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View cellView = convertView;

        // For reuse
        if (convertView == null) {
            cellView = inflater.inflate(R
                    .layout.view_calder_item, null);
        }

        TextView tv1 = (TextView) cellView.findViewById(R.id.tv1);
        ImageView typeIv = (ImageView) cellView.findViewById(R.id.typeIv);
        ImageView timeIv = (ImageView) cellView.findViewById(R.id.timeIv);
        tv1.setTextColor(Color.parseColor("#6e7b8c"));
        final DateTime dateTime = this.datetimeList.get(position);
//        if (dateTime.equals(getToday())) {
//            tv1.setBackgroundResource(R.drawable.cader_tv_today_bg);
//        }else{
//
//            if (selectDay.equals(dateTime.getYear()+"-"+dateTime.getMonth()+"-"+dateTime.getDay())){
//                tv1.setBackgroundResource(R.drawable.cader_tv_select_bg);
//            }else {
//                tv1.setBackgroundResource(R.drawable.caldroid_white);
//            }
//
//        }

        if (selectDay.equals(dateTime.getYear()+"-"+dateTime.getMonth()+"-"+dateTime.getDay())){
            tv1.setBackgroundResource(R.drawable.cader_tv_select_bg);
        }else {
            if (dateTime.equals(getToday())) {
                tv1.setBackgroundResource(R.drawable.cader_tv_today_bg);
            }else{
                tv1.setBackgroundResource(R.drawable.caldroid_white);

            }
        }


        tv1.setText("" + dateTime.getDay());



        if (dateTime.getMonth()<getToday().getMonth()){
            tv1.setTextColor((dateTime.getMonth() != month) ?Color.parseColor("#e1e4e6") : Color.parseColor("#b1b9c2"));
        }else if (dateTime.getMonth()==getToday().getMonth()){
            if (dateTime.getMonth() != month){
                tv1.setTextColor(Color.parseColor("#e1e4e6"));
            }else if (getToday().getDay()>dateTime.getDay()){
                tv1.setTextColor(Color.parseColor("#b1b9c2"));
            }else {
                tv1.setTextColor(Color.parseColor("#6e7b8c"));
            }

        }else{
            tv1.setTextColor((dateTime.getMonth() != month) ?Color.parseColor("#e1e4e6") : Color.parseColor("#6e7b8c"));
        }
        if (dateTime.getYear()<getToday().getYear()){
            tv1.setTextColor((dateTime.getMonth() != month) ?Color.parseColor("#e1e4e6") : Color.parseColor("#b1b9c2"));
        }
        if ((!ListUtil.isNullOrEmpty(complete))&&complete.contains(dateTime.getDay())&&dateTime.getMonth()==month){
            tv1.setTextColor(Color.parseColor("#3eede7"));
        }
        if ((!ListUtil.isNullOrEmpty(noComplete))&&noComplete.contains(dateTime.getDay())&&dateTime.getMonth()==month){
            tv1.setTextColor(Color.parseColor("#f47983"));
        }
        timeIv.setVisibility(View.GONE);
        if (hashMap!=null){
            if (hashMap.containsKey(dateTime.getDay())&&dateTime.getMonth()==month){
                timeIv.setVisibility(View.VISIBLE);
                int staues = hashMap.get(dateTime.getDay());
                if (dateTime.getDay()<getToday().getDay()&&dateTime.getMonth()<=getToday().getMonth()){
                    switch (staues){
                        case 0:
                            timeIv.setImageResource(R.drawable.cader2);
                            break;
                        case 1:
                            timeIv.setImageResource(R.drawable.cader6);

                            break;
                        case 2:
                            timeIv.setImageResource(R.drawable.cader6);
                            break;
                        case 3:
                            timeIv.setImageResource(R.drawable.caldroid_white);
                            break;
                    }
                }else{
                    switch (staues){
                        case 0:
                            timeIv.setImageResource(R.drawable.cader4);
                            break;
                        case 1:
                            timeIv.setImageResource(R.drawable.cader5);
                            break;
                        case 2:
                            timeIv.setImageResource(R.drawable.cader1);
                            break;
                        case 3:
                            timeIv.setImageResource(R.drawable.caldroid_white);
                            break;
                    }
                }
            }

        }




        cellView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dateTime.getMonth()<getToday().getMonth()){
                    return;
                }

                if (dateTime.getMonth()>getToday().getMonth()&&month==today.getMonth()){
                    return;
                }

                if ((dateTime.getMonth()==getToday().getMonth())&&dateTime.getDay()<getToday().getDay()){
                    return;
                }


                Intent intent = new Intent(DateFragment.DATE_CLICK_ACTION);
                intent.putExtra("day",dateTime.getDay());
                intent.putExtra("m",dateTime.getMonth());
                intent.putExtra("y",dateTime.getYear());
                context.sendBroadcast(intent);
                selectDay = dateTime.getYear()+"-"+dateTime.getMonth()+"-"+dateTime.getDay();
                notifyDataSetChanged();

            }
        });

        return cellView;
    }

    String selectDay="";

    List<Integer> complete;
    List<Integer> noComplete;
    public void orderStaues(List<Integer> complete, List<Integer> noComplete){
        this.complete = complete;
        this.noComplete = noComplete;
        notifyDataSetChanged();
    }

    HashMap<Integer,Integer> hashMap;
    public void  setTime(HashMap<Integer,Integer> hashMap){
     this.hashMap = hashMap;
     notifyDataSetChanged();
    }

    public HashMap<Integer,Integer> getTimeHas(){
        if (hashMap==null){
            hashMap = new HashMap<>();
        }
        return hashMap;
    }




}




